package com.apes.scm.stock.inventory.model;

import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.scm.masterdata.party.model.Party;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.party.repository.PartyRoleRepository;
import com.apes.scm.masterdata.stock.model.Location;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.*;

/**
 * 盘点表
 * @author yiny
 * @create 2018.9.15
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = InventoryList.CONST_TABLE_NAME)
public class InventoryList extends Model{
    public static final String CONST_TABLE_NAME = "sto_inventory_list";

    @Id
    @Column(name = "id",nullable = false, length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "盘点表", prefix = "PD%(company)%(yy)%(month)", padding = 4, useDateRange=true)
    private String id; // 盘点单号

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "warehouse_id", nullable = false, referencedColumnName = "id")
    private Location warehouseId; //仓库库区

    @Column(name = "note")
    private String note; // 备注

    @Column(name = "state", length = 10)
    private String state; // 单据状态（create:创建, delete:删除, approve:审核, inventory:盘点, compound:转审核, review:复审, "done":完成(不用复盘)）

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "inventory_uid", referencedColumnName = "id")
    private PartyRole inventoryUid; // 盘点人

    @Column(name = "inventory_date")
    private Date inventoryDate; // 盘点日期

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "approve_uid", referencedColumnName = "id")
    private PartyRole approveUid; // 审核人

    @Column(name = "approve_date")
    private Date approveDate; // 审核日期

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "delete_uid", referencedColumnName = "id")
    private PartyRole deleteUid; // 删除人

    @Column(name = "delete_date")
    private Date deleteDate; // 删除日期

    @Column(name = "compound_uid")
    private PartyRole compoundUid; // 复盘人

    @Column(name = "compound_date")
    private Date compoundDate; // 复盘日期

    @Column(name = "review_uid")
    private PartyRole reviewUid; // 复审人

    @Column(name = "review_date")
    private Date reviewDate; // 复审日期

    @Comment("公司")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "company_id", referencedColumnName = "id", nullable = false)
    private PartyRole company;

    // 盘点表明细
    @OneToMany(mappedBy = "inventoryListId", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<InventoryListItem> inventoryListItem = new ArrayList<>();

    public List<InventoryListItem> getInventoryListItem() {
        if (inventoryListItem == null) {
            return new ArrayList<>();
        }
        for (InventoryListItem item:inventoryListItem) {
            item.setInventoryListId(this);
        }
        return inventoryListItem;
    }

    // 盘点差异复盘
    @OneToMany(mappedBy = "inventoryListIds", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<InventoryDiffCompound> inventoryDiffCompound = new ArrayList<>();

    public List<InventoryDiffCompound> getInventoryDiffCompound() {
        if (inventoryDiffCompound == null) {
            return new ArrayList<>();
        }
        for (InventoryDiffCompound diffCompound:inventoryDiffCompound) {
            diffCompound.setInventoryListIds(this);
        }
        return inventoryDiffCompound;
    }

    @Transient
    private PartyRole _creator;

    @Transient
    public PartyRole get_creator() {
        if(this.getCreator() == null) return null;
        return SpringManager.getBean(PartyRoleRepository.class).findOne(this.getCreator());
    }
}













